﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Input;
using CPF.Mac;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using CPFUI.IControl.KeyBord;
using CPFUI.IHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class KeyBorderLetter : Control
    {
        KeyBorderItem KeyItemLShift;
        KeyBorderItem KeyItemRShift;
        KeyBorderItem KeyItemCapsLock;
        KeyBorderItem KeyItemLControl;
        KeyBorderItem KeyItemRControl;
        KeyBorderItem KeyItemLAlt;
        KeyBorderItem KeyItemRAlt;
        KeyBorderItem KeyItemWin;
        StackPanel pnlCharContent;
        bool isupper = false;
        bool isNormal = true;
        List<Keys> pressedModifierKeyCodes = null;

        /// <summary>
        /// 按下的组合键集合
        /// </summary>
        List<Keys> PressedModifierKeyCodes
        {
            get
            {
                if (pressedModifierKeyCodes == null)
                {
                    pressedModifierKeyCodes = new List<Keys>();
                }
                return pressedModifierKeyCodes;
            }
        }
        //模板定义
        protected override void InitializeComponent()
        {
            pnlCharContent = new StackPanel() { 
            MarginLeft=0,
            MarginBottom=0,
            MarginTop=0,
            MarginRight=0};
            
            Children.Add(pnlCharContent);
            pnlCharContent.Children.Add(new StackPanel
            {
                Height=65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {
                    new KeyBorderItem(Keys.Escape,"Esc","")
                    {
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Oem3,"`","~")
                    {
                        MarginLeft=0,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D1,"1","!")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D2,"2","@")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D3,"3","#")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D4,"4","$")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D5,"5","%")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D6,"6","^")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D7,"7","&")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D8,"8","*")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D9,"9","(")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D0,"0",")")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.OemMinus,"-","_")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Oemplus,"=","+")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=52,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Back,"Bksp","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=105,
                        Height=65,
                    },
                    
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                Height = 65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {

                    new KeyBorderItem(Keys.Tab,"Tab","")
                    {
                        IsChar= false,
                        Width=105,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Q,"q","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.W,"w","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.E,"e","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.R,"r","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.T,"t","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Y,"y","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.U,"u","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.I,"i","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.O,"o","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.P,"p","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.OemOpenBrackets,"[","{")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Oem6,"]","}")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=65,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Oem5,"\\","|")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=87,
                        Height=65,
                    },
                    
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                Height = 65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {

                    new KeyBorderItem(Keys.CapsLock,"Caps","")
                    {

                        Name="KeyItemCapsLock",
                        PresenterFor=this,
                        IsChar= false,
                        Width=130,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.A,"a","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.S,"s","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.D,"d","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.F,"f","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.G,"g","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.H,"h","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.J,"j","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.K,"k","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.L,"l","{")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Oem1,";",":")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Oem7,"'","\"")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Enter,"Enter","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=144,
                        Height=65,
                    },
                    
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                Height = 65,
                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {

                    new KeyBorderItem(Keys.ShiftKey,"Shift","")
                    {
                        Name="KeyItemLShift",
                        PresenterFor=this,
                        IsChar= false,
                        Width=155,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Z,"z","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.X,"x","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.C,"c","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.V,"v","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.B,"b","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.N,"n","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.M,"m","")
                    {
                        MarginLeft=2,
                        IsChar= true,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Oemcomma,",","<")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.OemPeriod,".",">")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.OemQuestion,"/","?")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=50,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.ShiftKey,"Shift","")
                    {
                        Name="KeyItemRShift",
                        PresenterFor=this,
                        MarginLeft=2,
                        IsChar= false,
                        Width=171,
                        Height=65,
                    },
                    
                }
            });

            pnlCharContent.Children.Add(new StackPanel
            {

                MarginTop = 2,
                MarginLeft = 0,
                Orientation = Orientation.Horizontal,
                Children =
                {

                    new KeyBorderItem(Keys.ControlKey,"Ctrl","")
                    {
                        Name="KeyItemLControl",
                        PresenterFor=this,
                        IsChar= false,
                        Width=80,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.LWin,"Win","")
                    {
                        Name="KeyItemWin",
                        PresenterFor=this,
                        MarginLeft=2,
                        IsChar= false,
                        Width=80,
                        Height=65,
                    },new KeyBorderItem(Keys.Alt,"Alt","")
                    {
                        Name="KeyItemLAlt",
                        PresenterFor=this,
                        MarginLeft=2,
                        IsChar= false,
                        Width=80,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Space,"空格","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=356,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Alt,"Alt","")
                    {
                        Name="KeyItemRAlt",
                        PresenterFor=this,
                        MarginLeft=2,
                        IsChar= false,
                        Width=80,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.Apps,"右键","")
                    {
                        MarginLeft=2,
                        IsChar= false,
                        Width=80,
                        Height=65,
                    },
                    new KeyBorderItem(Keys.ControlKey,"Ctrl","")
                    {
                        Name="KeyItemRControl",
                        PresenterFor=this,
                        MarginLeft=2,
                        IsChar= false,
                        Width=80,
                        Height=65,
                    },

                    
                }
            });


        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();
            for (int i = 0; i < pnlCharContent.Children.Count; i++)
            {
                StackPanel pnl = (StackPanel)pnlCharContent.Children[i];
                if (pnl == null)
                {
                    continue;
                }
                for (int j = 0; j < pnl.Children.Count; j++)
                {
                    KeyBorderItem txtkey = pnl.Children[j] as KeyBorderItem;
                    if (txtkey == null)
                    {
                        continue;
                    }
                    txtkey.MouseDown += KeyBorder_MouseDown;
                }
            }
            KeyItemLShift = this.FindPresenterByName<KeyBorderItem>("KeyItemLShift");
            KeyItemRShift = this.FindPresenterByName<KeyBorderItem>("KeyItemRShift");
            KeyItemLControl = this.FindPresenterByName<KeyBorderItem>("KeyItemLControl");
            KeyItemRControl = this.FindPresenterByName<KeyBorderItem>("KeyItemRControl");
            KeyItemLAlt = this.FindPresenterByName<KeyBorderItem>("KeyItemLAlt");
            KeyItemRAlt = this.FindPresenterByName<KeyBorderItem>("KeyItemRAlt");
            KeyItemLShift = this.FindPresenterByName<KeyBorderItem>("KeyItemLShift");
            KeyItemCapsLock = this.FindPresenterByName<KeyBorderItem>("KeyItemCapsLock");
            KeyItemWin = this.FindPresenterByName<KeyBorderItem>("KeyItemWin");
            
            short capsLockState = UnsafeNativeMethods.GetKeyState((int)Keys.CapsLock);
            KeyItemCapsLock.IsPressed = (capsLockState & 0x0001) != 0?Visibility.Visible:Visibility.Hidden;
        }

        private void KeyBorder_MouseDown(object sender, CPF.Input.MouseButtonEventArgs e)
        {
            KeyBorderItem keyItem = sender as KeyBorderItem;
            if (keyItem == null)
            {
                return;
            }
            SyncKeyPairs(keyItem);
            if (ProcessSpecialKey(keyItem))
            {
                return;
            }
            if (keyItem.KeyCode == Keys.NumLock || keyItem.KeyCode == Keys.ShiftKey || keyItem.KeyCode == Keys.CapsLock)
            {
                ValueChange(KeyItemLShift.IsPressed==Visibility.Visible, true, KeyItemCapsLock.IsPressed == Visibility.Visible);
            }
            if (keyItem.IsLockKey)
            {
                KeyboardInput.SendToggledKey(keyItem.KeyCode);
            }
            else if (keyItem.IsModifierKey)
            {
                if (PressedModifierKeyCodes.Contains(keyItem.KeyCode))
                {
                    if (keyItem.KeyCode == Keys.ShiftKey|| keyItem.KeyCode == Keys.LWin)
                    {
                        KeyboardInput.SendToggledKey(keyItem.KeyCode);
                    }
                    PressedModifierKeyCodes.Remove(keyItem.KeyCode);
                }
                else
                {
                    PressedModifierKeyCodes.Add(keyItem.KeyCode);
                }
            }
            else if (keyItem.IsIMEKey)
            {
                IntPtr hw = UnsafeNativeMethods.GetForegroundWindow();
                //如果是中英文切换按键
                //KeyboardInput.SendToggledKey(Keys.ShiftKey);
                //keyItem.NormalText= hw.ToString();
                //keyItem.UpdateDisplayText(KeyItemLShift.IsPressed == Visibility.Visible, KeyItemNumLock.IsPressed == Visibility.Visible, KeyItemCapsLock.IsPressed == Visibility.Visible);
                UnsafeNativeMethods.SimulateShiftKeyPress(hw);
            }
            else
            {
                Keys btnKeyCode = keyItem.KeyCode;

                //如果该键是数字键盘键且未按下NumLock，则使用
                if (keyItem.IsNumberPadKey && !(true) && keyItem.UnNumLockKeyCode > 0)
                {
                    btnKeyCode = (Keys)keyItem.UnNumLockKeyCode;
                }

                KeyboardInput.SendKey(PressedModifierKeyCodes, btnKeyCode);

                //清除所有组合键按钮的按下状态。
                ResetModifierKeyButtons();
            }
        }
        string languge = "zh-CN";
        void ResetModifierKeyButtons()
        {
            PressedModifierKeyCodes.Clear();
            KeyItemLShift.IsPressed = Visibility.Hidden;
            KeyItemRShift.IsPressed = Visibility.Hidden;
            KeyItemRControl.IsPressed = Visibility.Hidden;
            KeyItemLControl.IsPressed = Visibility.Hidden;
            KeyItemRAlt.IsPressed = Visibility.Hidden;
            KeyItemLAlt.IsPressed = Visibility.Hidden;
            KeyItemWin.IsPressed = Visibility.Hidden;

            ValueChange(false, true, KeyItemCapsLock.IsPressed == Visibility.Visible);
        }
        //用户代码

        /// <summary>
        /// 改变切换字符
        /// </summary>
        private void ValueChange(bool isShiftKeyPressed, bool isNumLockPressed, bool isCapsLockPressed)
        {
            for (int i = 0; i < pnlCharContent.Children.Count; i++)
            {
                StackPanel pnl = (StackPanel)pnlCharContent.Children[i] ;
                if (pnl == null)
                {
                    continue;
                }
                for (int j = 0; j < pnl.Children.Count; j++)
                {
                    KeyBorderItem keyitem = pnl.Children[j] as KeyBorderItem;
                    if (keyitem == null)
                    {
                        continue;
                    }
                    
                    keyitem.UpdateDisplayText(isShiftKeyPressed, isNumLockPressed, isCapsLockPressed);
                }
            }
        }

        void SyncKeyPairs(KeyBorderItem item)
        {
            if (item == KeyItemLShift)
            {
                KeyItemRShift.IsPressed = KeyItemLShift.IsPressed;
            }
            if (item == KeyItemRShift)
            {
                KeyItemLShift.IsPressed = KeyItemRShift.IsPressed;
            }

            if (item == KeyItemLAlt)
            {
                KeyItemRAlt.IsPressed = KeyItemLAlt.IsPressed;
            }
            if (item == KeyItemRAlt)
            {
                KeyItemLAlt.IsPressed = KeyItemRAlt.IsPressed;
            }

            if (item == KeyItemLControl)
            {
                KeyItemRControl.IsPressed = KeyItemLControl.IsPressed;
            }
            if (item == KeyItemRControl)
            {
                KeyItemLControl.IsPressed = KeyItemRControl.IsPressed;
            }
        }
        bool ProcessSpecialKey(KeyBorderItem btn)
        {
            bool handled = true;
            switch (btn.KeyCode)
            {

                // Use Shift+F10 to simulate the Apps key. 
                case Keys.Apps:
                    KeyboardInput.SendKey(
                                        new Keys[] { Keys.ShiftKey },
                                        Keys.F10);
                    break;
                default:
                   handled = false;
                    break;
            }
            return handled;
        }

#endif
    }
}
